home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / CLOCK.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  6KB  |  202 lines

  1. /* CLOCK.H:  prototypes/defines for the CLOCK.C file                    */
  2. /*
  3.     This file written in 1990 Jonathan R. Guthrie and placed in the
  4.     public domain
  5. */
  6.  
  7. int     far     startclock(int x, int y, int attr);
  8. void    far     stopclock(void);
  9.  
  10. #define CLOCK_OK        0   /* Clock was installed properly             */
  11. #define CLOCK_ERR_INST  -1  /* Clock is already installed               */
  12. #define CLOCK_ERR_OOS   -2  /* Clock would be off the screen            */
  13.  
  14.  
  15. /* CLOCK.C:  An on-screen clock generator                               */
  16. /*
  17.     This file written in 1990 Jonathan R. Guthrie and placed in the
  18.     public domain
  19. */
  20.  
  21. #include    <stdlib.h>
  22. #include    <conio.h>
  23. #include    <time.h>
  24. #include    <dos.h>
  25. #include    "clock.h"
  26.  
  27. #define CLOCK       0x1c
  28. #define TRUE        1
  29. #define FALSE       0
  30.  
  31. /* Now, the stuff needed for the for the on-screen clock                */
  32.  
  33. static  void    interrupt   (*oldvector)(void);
  34. static  void    interrupt   do_clock(void);
  35. static  void    setstr(char *, int, size_t);
  36.  
  37. static  char    timestuff[22], hours, oddfives, ticks, ticklimit;
  38.  
  39. static  int     clockx, clocky, clockattr, installed = FALSE;
  40.  
  41. int     far     startclock(int x, int y, int attr)
  42. {
  43.       time_t  temptime;
  44.       struct  tm  *struct_time;
  45.       int     bigx;
  46.       struct  text_info   r;
  47.  
  48.       /* The clock starting routine */
  49.  
  50.       /* First, see if it's already installed */
  51.  
  52.       if(installed)
  53.             return  CLOCK_ERR_INST;
  54.       else  installed = TRUE;
  55.  
  56.       /* Now, set assorted important module constants */
  57.  
  58.       clockx = x;
  59.       clocky = y;
  60.       clockattr = attr;
  61.  
  62.       gettextinfo(&r);
  63.       bigx = (r.currmode < 2) ? 40 : 80;
  64.       bigx = bigx - 10;
  65.  
  66.       if((clockx < 0) || (clockx > bigx) || (clocky < 0) || (clocky > 25))
  67.       {
  68.             installed = FALSE;
  69.             return CLOCK_ERR_OOS;
  70.       }
  71.  
  72.       /* Now, set the program's clock */
  73.  
  74.       setstr(timestuff, clockattr, 22);
  75.  
  76.       time(&temptime);
  77.       struct_time = localtime(&temptime);
  78.  
  79.       oddfives    = 0;
  80.       ticklimit   = 17;
  81.       hours         = (struct_time->tm_hour + 11) % 12 + 1;
  82.       timestuff[0]  = (hours > 9) ? '1' : ' ';
  83.       timestuff[2]  = '0' + hours % 10;
  84.       timestuff[4]  = ':';
  85.       timestuff[6]  = '0' + struct_time->tm_min / 10;
  86.       timestuff[8]  = '0' + struct_time->tm_min % 10;
  87.       timestuff[10] = ':';
  88.       timestuff[12] = '0' + struct_time->tm_sec / 10;
  89.       timestuff[14] = '0' + struct_time->tm_sec % 10;
  90.       timestuff[16] = ' ';
  91.       timestuff[18] = (struct_time->tm_hour > 11) ? 'P' : 'A';
  92.       timestuff[20] = 'M';
  93.  
  94.       /* Now, initialize the clock as displayed on the screen */
  95.  
  96.       puttext(clockx, clocky, clockx+10, clocky, timestuff);
  97.  
  98.       /* Finally, set the vector to point to the clock routine */
  99.  
  100.       disable();
  101.       oldvector = getvect(CLOCK);
  102.       setvect(CLOCK, do_clock);
  103.       enable();
  104.       return  CLOCK_OK;
  105. }
  106.  
  107. static  void    interrupt   do_clock(void)
  108. {
  109.       ++ticks;
  110.       if(ticks > ticklimit)   /* Then, it's time to update the seconds */
  111.       {
  112.             ticks = 0;
  113.  
  114.             /* First, handle the fractional Hz part */
  115.  
  116.             ++oddfives;
  117.             if (5 == oddfives)
  118.             {
  119.                   oddfives = 0;
  120.                   ticklimit = 18;
  121.             }
  122.             else
  123.             {
  124.                   ticklimit = 17;
  125.             }
  126.    
  127.             /* Now, handle the seconds count */
  128.  
  129.             ++timestuff[14];
  130.  
  131.             if (timestuff[14] > '9')
  132.             {
  133.                   timestuff[14] = '0';
  134.  
  135.                   /* Now, handle the tens of seconds count */
  136.  
  137.                   ++timestuff[12];
  138.                   if (timestuff[12] > '5')
  139.                   {
  140.                         timestuff[12] = '0';
  141.  
  142.                         /* Now, handle the minutes count */
  143.  
  144.                         ++timestuff[8];
  145.                         if (timestuff[8] > '9')
  146.                         {
  147.                               timestuff[8] = '0';
  148.  
  149.                               /* Now, handle the ten minutes count */
  150.  
  151.                               ++timestuff[6];
  152.                               if (timestuff[6] > '5')
  153.                               {
  154.                                     timestuff[6] = '0';
  155.  
  156.                                     /* Now, handle the hours count */
  157.  
  158.                                     ++hours;
  159.                                     if(12 == hours)
  160.                                           if ('P' == timestuff[18])
  161.                                                 timestuff[18] = 'A';
  162.                                           else  timestuff[18] = 'P';
  163.  
  164.                                     if(hours > 12)
  165.                                           hours = 1;
  166.  
  167.                                     timestuff[0] = (hours > 9) ? '1' : ' ';
  168.                                     timestuff[2] = '0' + hours % 10;
  169.                               }
  170.                         }
  171.                   }
  172.             }
  173.  
  174.             /* Now, update the display */
  175.  
  176.             puttext(clockx, clocky, clockx+10, clocky, timestuff);
  177.       }
  178. }
  179.  
  180. void    far     stopclock(void)
  181. {
  182.       if(installed)
  183.       {
  184.             disable();
  185.             setvect(CLOCK, oldvector);
  186.             enable();
  187.             installed = FALSE;
  188.       }
  189. }
  190.  
  191. static  void    setstr(char *s, int ch, size_t n)
  192. {
  193.       size_t  i;
  194.     
  195.       for(i=0 ; i<n ; ++i)
  196.       {
  197.             s[i] = ch;
  198.       }
  199.  
  200.       s[i] = '\0';
  201. }
  202.